/**
 *
 * Date: 2020/08/20 Time: 10:06
 *
 * 换房报表
 */

import React, {Component} from 'react';
import {
    Linking,
    SafeAreaView,
    ScrollView,
    TouchableOpacity,
    View,
    NativeModules,
    TouchableHighlight,
    Text,
    Platform,
    BackHandler,
    FlatList,
    ToastAndroid,
} from 'react-native';
import {connect} from 'react-redux';
import {styles} from './roomChangeReport.Style';
import {FONT_COLOR_TITLE} from "../../../constant/PHColors";
import AntDesign from "react-native-vector-icons/AntDesign";
import SelectDatePlugIn from "../../custom/selectDateTime/selectDatePlugIn";
import {Row, Rows, Table} from 'react-native-table-component';
import {retrieveRoomChangeReport} from "./Actions"
import {FetchStatus} from "../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../configuration/ReturnStatus";
import {retrieveWHSKShiftsArrangeDefault} from "../../discover/staffSchedule/Actions";
import {doDisplayWarnMessage} from "../../../component/Message/Action";

class roomChangeReport extends Component {
    static navigationOptions = ({navigation}) => ({
        headerTitle: ``,
        headerLeft: <View><Text style={{marginLeft:15,fontSize:18,color:FONT_COLOR_TITLE}}>换房报表</Text></View>,
        headerRight: '',
    });

    // 构造
    constructor(props) {
        super(props);

        let date = new Date();
        let year = date.getFullYear().toString();
        let month = (date.getMonth()+1).toString();
        if(Number(month)<10){
            month = '0'+month
        }
        let day = date.getDate().toString();
        if(Number(day)<10){
            day = '0'+day
        }
        let opdate = year+'-'+month+'-'+day;

        // 初始状态
        this.state = {
            startdate:opdate,
            enddate:opdate,
            reportData:[],
        };

        this.tableHeader = ["日期","时间","客人","原房号","原房型","新房号","新房型","房价","现房价","余额","备注","操作员","抵店日期","离店日期"];
        this.tableWidth = [100,70,150,80,120,80,120,80,80,100,200,120,100,100];
    }

    componentWillReceiveProps(nextProps, nextContext): void {
        let {dispatch} = this.props;
        if (nextProps.onRetrieveRoomChangeReport !== this.props.onRetrieveRoomChangeReport && nextProps.onRetrieveRoomChangeReport.fetchStatus === FetchStatus.Success) {
            if (nextProps.onRetrieveRoomChangeReport.RETURN === ReturnStatus.Success) {
                let rows = nextProps.onRetrieveRoomChangeReport.DATA;
                let data = [];
                rows.map((item,index)=>{
                    let data_son = [];
                    let opdate = item.opdate;
                    data_son.push(opdate.substring(0,4)+'-'+opdate.substring(4,6)+'-'+opdate.substring(6,8));
                    data_son.push(item.time);
                    data_son.push(item.gname);
                    data_son.push(item.oldroom);
                    data_son.push(item.oldrmtype);
                    data_son.push(item.newroom);
                    data_son.push(item.newrmtype);
                    data_son.push(item.log_ncard);
                    data_son.push(item.rate);
                    data_son.push(item.balance);
                    data_son.push(item.text);
                    data_son.push(item.userid);
                    let arr_d = item.arr_d;
                    data_son.push(arr_d.substring(0,4)+'-'+arr_d.substring(4,6)+'-'+arr_d.substring(6,8));
                    let exp_dep_d = item.exp_dep_d;
                    data_son.push(exp_dep_d.substring(0,4)+'-'+exp_dep_d.substring(4,6)+'-'+exp_dep_d.substring(6,8));
                    data.push(data_son)
                });
                this.setState({
                    reportData:data,
                })
                this.reportData = data;
            }else{
                dispatch(doDisplayWarnMessage(nextProps.onRetrieveRoomChangeReport.MSG))
            }
        }
    }

    componentWillMount() {
        this.props.navigation.setParams({downloadExcel: this.downloadExcel});
    };

    // 组件渲染后调用
    componentDidMount() {
        let {dispatch} = this.props;

        let reg = new RegExp("-","g");
        let startdate =(this.state.startdate.replace(reg,""));
        let enddate =(this.state.enddate.replace(reg,""));
        let str = {"where":{"all_search":"","startdate":startdate,"enddate":enddate},"arg":{"selectname":"report_room_move","fuzzy":"yes"},"radio":{},"checkbox":{}}
        dispatch(retrieveRoomChangeReport(JSON.stringify(str)));
    }

    downloadExcel=()=>{

    }

    render() {
        return (
            <SafeAreaView style={styles.body}>
                <View style={styles.opdateView}>
                    <AntDesign class={'aaa'} name={'clockcircleo'} size={16} style={{color: '#d7d7d7', marginRight: 10}} />
                    <SelectDatePlugIn defaultDate={this.state.startdate} textStyle={styles.opdateViewStyle} dateBack={(date)=>{
                        this.setState({startdate:date});
                        let {dispatch} = this.props;

                        let reg = new RegExp("-","g");
                        let startdate =(date.replace(reg,""));
                        let enddate =(this.state.enddate.replace(reg,""));
                        let str = {"where":{"all_search":"","startdate":startdate,"enddate":enddate},"arg":{"selectname":"report_room_move","fuzzy":"yes"},"radio":{},"checkbox":{}}
                        dispatch(retrieveRoomChangeReport(JSON.stringify(str)));

                    }} />
                    <Text style={styles.opdateViewStyle}>   -   </Text>
                    <SelectDatePlugIn defaultDate={this.state.enddate} textStyle={styles.opdateViewStyle} dateBack={(date)=>{
                        this.setState({enddate:date});
                        let {dispatch} = this.props;

                        let reg = new RegExp("-","g");
                        let startdate =(this.state.startdate.replace(reg,""));
                        let enddate =(date.replace(reg,""));
                        let str = {"where":{"all_search":"","startdate":startdate,"enddate":enddate},"arg":{"selectname":"report_room_move","fuzzy":"yes"},"radio":{},"checkbox":{}}
                        dispatch(retrieveRoomChangeReport(JSON.stringify(str)));

                    }} />
                </View>

                <ScrollView style={styles.body}>
                    <FlatList
                        data={[this.state.reportData]}
                        keyExtractor={(item, index) => index}
                        renderItem={this.renderTableItem}
                        horizontal={true}
                        showsHorizontalScrollIndicator={false}
                    />
                </ScrollView>
            </SafeAreaView>
        )
    }

    renderTableItem=({item})=>{
        let _this = this;
        console.log("reportData-----------"+JSON.stringify(item));
        return(
            <Table style={{marginLeft: 0}}>
                <Row data={_this.tableHeader} widthArr={_this.tableWidth} style={styles.tableHeader} textStyle={styles.tableText}/>
                <Rows data={item} widthArr={_this.tableWidth} style={styles.tableHeader} textStyle={styles.tableText}/>
            </Table>
        )
    }
}

const props = state => {
    return {
        onRetrieveRoomChangeReport:state.onRetrieveRoomChangeReport,
    }
};

export default roomChangeReport = connect(props)(roomChangeReport);